﻿Ext.define('page.Room.Mod', {
    extend: 'Kwan.ex.ModuleInstance',
    metas: [
        { name: "RoomNO", label: "房号", sort: 1, ex_list: { width: 60 }, ex_edit: { allowBlank: false, width: 250} },
        { name: "ClassName", label: "房型", sort: 3, ex_list: { width: 80 }, ex_edit: { xtype: 'treecombo', ex_combo: { ds: 'RoomTree', field: 'Cid' }, cfg: { allowBlank: false, labelWidth: 70, width: 200}} },
        { name: "Floor", label: "楼层", sort: 5, ex_edit: { allowBlank: false, width: 250} },
        { name: "Deposit", label: "押金", sort: 12, ex_edit: { xtype: 'numberfield', hideTrigger: true, allowBlank: true, width: 250} },
        { name: "IsComputer", label: "电脑", sort: 9, ex_list: { width: 60, renderer: Kwan.colorHas }, ex_edit: { xtype: 'checkboxfield', group: 1, cfg: { width: 250}} },
        { name: "IsMaQue", label: "麻雀", sort: 11, ex_list: { width: 60, renderer: Kwan.colorHas }, ex_edit: { xtype: 'checkboxfield', group: 1, cfg: { width: 250}} },
        { name: "Status", label: "状态", sort: 7, ex_list: { width: 60, renderer: Kwan.colorRoomStatus }, ex_edit: { xtype: 'SimpleCombo', ex_combo: { ds: Kwan.f_status, def: 0 }, cfg: { allowBlank: false, labelWidth: 70, width: 200}} },
        { name: "Price", label: "单价", sort: 2, ex_list: { width: 60 }, ex_edit: { xtype: 'numberfield', hideTrigger: true, width: 250} },
        { name: "HalfPrice", label: "半天价", sort: 4, ex_list: { width: 60 }, ex_edit: { xtype: 'numberfield', hideTrigger: true, width: 250} },
        { name: "HourPrice", label: "钟点价", sort: 6, ex_list: { width: 60 }, ex_edit: { xtype: 'numberfield', hideTrigger: true, width: 250} },
        { name: "NightPrice", label: "夜房价", sort: 8, ex_list: { width: 60 }, ex_edit: { xtype: 'numberfield', hideTrigger: true, width: 250} },
        { name: "Reserve", label: "最低价", sort: 10, ex_edit: { xtype: 'numberfield', hideTrigger: true, width: 250} },
        { name: "CreatedDate", label: "创建日期", sort: 12, ex_list: { width: 120, xtype: 'datecolumn', format: 'Y-m-d' }, ex_edit: { xtype: 'datefield', width: 250, hidden: true} }
	    ],
    classMetas: [
        { name: "NameCN", label: "名称", sort: 1, ex_edit: { allowBlank: false, width: 250} },
        { name: "Price", label: "价格", sort: 2, ex_edit: { allowBlank: false, width: 250} },
        { name: "HourPrice", label: "钟点价", sort: 3, ex_edit: { allowBlank: false, width: 250} },
        { name: "HalfPrice", label: "半天价", sort: 4, ex_edit: { allowBlank: false, width: 250} },
        { name: "NightPrice", label: "夜房价", sort: 5, ex_edit: { allowBlank: false, width: 250} },
        { name: "Reserve", label: "最低价", sort: 6, ex_edit: { allowBlank: false, width: 250} },
        { name: "Sort", label: "排序", sort: 7, ex_edit: { width: 250} }
	    ],
    cfg: { start: 0, limit: 20, facade: 'facade.axd?s_action=Room', def: 'Room' },
    newData: [{ Cid: '', CreatedDate: new Date(), CustomerName: '', Deposit: 0, Floor: '', HalfPrice: 0, HourPrice: 0, IsComputer: false, IsMaQue: false, NightPrice: 0, Price: 0, Reserve: 0, RoomNO: '', Status: 0, ClassName: '', ClassPrice: 0, ClassReserve: 0, ClassHalfPrice: 0, ClassNightPrice: 0, ClassHourPrice: 0}],
    classCfg: { facade: 'facade.axd?s_action=RoomClass', def: 'RoomClass' },
    classNewData: [{ HalfPrice: 0, HourPrice: 0, NightPrice: 0, NameCN: "", NameEN: "", Pid: 0, Price: 0, Reserve: 0, Sort: 0}],
    getGrid: function () {
        var me = this,
            sorters = [{ property: 'RoomNO', direction: 'ASC'}];
        var grid = Ext.create('Kwan.ex.Grid', {
            id: 'gpRoom',
            region: 'center',
            store: Kwan.createStore(this.metas, this.cfg, sorters),
            columns: Kwan.createColumn(this.metas),
            title: '房间列表',
            getQuery: function (clear) {
                var roomNO = Ext.getCmp('qRoomNO').getValue(),
                    Cid = Ext.getCmp('qCid').getValue();
                if (clear) {
                    Cid = roomNO = '';
                    Ext.getCmp('RoomTree').getSelectionModel().deselectAll();
                }
                if (roomNO.length > 0 || Cid.length > 0) {
                    this.store.proxy.extraParams = { query1: 'RoomNO=' + roomNO + '&Cid=' + Cid };
                } else { this.store.proxy.extraParams = null; }
                this.store.load();
                this.filters.bindStore(this.store);
            },
            operBar: Ext.create('Kwan.ex.CrudBar', {
                pushItems: [
                    { xtype: 'hidden', id: 'qCid', name: 'qCid' },
                    { scale: 'medium', height: 25, width: 100, emptyText: " 搜索房号..", xtype: 'textfield', id: 'qRoomNO', name: 'qRoomNO' },
                    { scale: 'medium', text: '搜索', icon: "/Admin/style/24Icons/jiancha.png", name: 'btnSearch',
                        handler: function () { Ext.getCmp('gpRoom').getQuery(); }
                    }
                ]
            })
        });
        grid.operBar.addListener('operAdd', function handler() {
            me.newData[0].CreatedDate = new Date();
            var cfg = { operType: 'add', saveUrl: me.cfg.facade, loadUrl: null, objId: null },
                win = me.getWin(cfg);
            win.show();
        });
        grid.operBar.addListener('operEdit', function handler() {
            var id = GetGridSelectedRowData(grid).EntityID;
            var cfg = { operType: 'edit', saveUrl: me.cfg.facade, loadUrl: me.cfg.facade, objId: id };
            var win = me.getWin(cfg);
            win.show();
        });
        grid.operBar.addListener('operDel', function handler() {
            var selects = grid.getSelectionModel().getSelection(), ids = [];
            if (selects.length > 0) {
                Ext.each(selects, function (item) {
                    ids.push(item.data.EntityID);
                });
                var cfg = { url: me.cfg.facade, id: ids.join(","), store: grid.getStore() };
                DoDelete(cfg);
            } else {
                ShowInfoMsg('系统提示', '请至少选择一项！');
            }
        });
        grid.operBar.addListener('operRefresh', function handler() {
            grid.getStore().load();
        });
        grid.addListener('itemdblclick', function handler(grid, rec, item, index, e) {
            var cfg = { operType: 'edit', saveUrl: me.cfg.facade, loadUrl: me.cfg.facade, objId: rec.data.EntityID };
            var win = me.getWin(cfg);
            win.show();
        });
        return grid;
    },
    getWin: function (cfg) {
        var win = Ext.create('Kwan.ex.FormWin', {
            title: '房间管理',
            width: 550,
            height: 290,
            operType: cfg.operType, // 操作类型,
            fpID: this.cfg.def,
            formWinItems: Kwan.createFormItems(this.metas, this.cfg.def), // 表单包含的字段
            //formWinItems: this.formItems(this.cfg.def),
            formColumns: 2, // 有几列
            //formWinLabelWidth : 100,// 字段label的宽度
            formWinFieldWidth: 800, // 字段的总宽度
            saveUrl: cfg.saveUrl, // 提交的url地址
            loadUrl: cfg.loadUrl, // 读取数据的Url地址
            fPanel: 'gpRoom', // 操作的父页面对象
            objId: cfg.objId, // 要修改的数据的id
            disableFields: [], // 编辑时要禁用的字段['Text', 'Code']
            newData: this.newData
        });
        return win;
    },
    getClassTree: function () {
        var me = this,
            nodeID = '';
        var contextMenu = Ext.create('Ext.menu.Menu', {
            width: 100,
            height: 100,
            margin: '0 0 10 0',
            floating: true,  // usually you want this set to True (default)
            items: [
            //{ itemId: 'miAdd', text: '添加子类别' }, '-',
                {itemId: 'miEdit', text: '修改' },
                { itemId: 'miDel', text: '删除' }
            ],
            listeners: {
                click: {
                    fn: function (menu, mi) {
                        switch (mi.itemId) {
                            case "miAdd":
                                var newData = [{ HalfPrice: 0, HourPrice: 0, NightPrice: 0, NameCN: "", NameEN: "", Pid: nodeID, Price: 0, Reserve: 0, Sort: 0}];
                                var cfg = { operType: 'add', saveUrl: me.classCfg.facade, loadUrl: null, objId: null, newData: newData };
                                var win = me.getClassWin(cfg);
                                win.show(); break;
                            case "miEdit":
                                if (nodeID.length > 0) {
                                    var cfg = { operType: 'edit', saveUrl: me.classCfg.facade, loadUrl: me.classCfg.facade, objId: nodeID };
                                    var win = me.getClassWin(cfg);
                                    win.show();
                                } break;
                            case "miDel":
                                if (nodeID.length > 0) {
                                    var cfg = { url: 'facade.axd?s_action=RoomClass', id: nodeID, store: tree.getStore() };
                                    DoDelete(cfg);
                                } break;
                        }
                    }
                }
            }
        });
        var tree = Ext.create('Kwan.ex.ClassTree', {
            id: 'RoomTree',
            ajaxUrl: 'facade.axd?s_action=RoomClassTree',
            title: '房间类别',
            viewConfig: {
                stripeRows: true,
                listeners: {
                    itemcontextmenu: function (view, rec, node, index, e) {
                        e.stopEvent();
                        nodeID = rec.data.id;
                        contextMenu.showAt(e.getXY());
                        return false;
                    }
                }
            },
            dockedItems: [{
                xtype: 'toolbar',
                items: [{ scale: 'medium', text: '新增', icon: "/Admin/style/24Icons/plus.png",
                    handler: function () {
                        me.classNewData[0].Pid = nodeID;
                        var cfg = { operType: 'add', saveUrl: me.classCfg.facade },
                            win = me.getClassWin(cfg);
                        win.show();
                    }
                }]
            }],
            listeners: {
                'itemclick': function (view, re) {
                    Ext.getCmp('qCid').setValue(re.data.id);
                    Ext.getCmp('gpRoom').getQuery();
                }
            }
        });
        return tree;
    },
    getClassWin: function (cfg) {
        var win = Ext.create('Kwan.ex.FormWin', {
            title: '房间类别管理',
            width: 550,
            height: 290,
            operType: cfg.operType, // 操作类型,
            fpID: cfg.def,
            formWinItems: Kwan.createFormItems(this.classMetas, cfg.def), // 表单包含的字段
            formColumns: 2, // 有几列
            formWinFieldWidth: 800, // 字段的总宽度
            saveUrl: cfg.saveUrl, // 提交的url地址
            loadUrl: cfg.loadUrl, // 读取数据的Url地址
            fPanel: 'RoomTree', // 操作的父页面对象
            objId: cfg.objId, // 要修改的数据的id
            disableFields: [], // 编辑时要禁用的字段['Text', 'Code']
            newData: this.classNewData
        });
        return win;
    },
    run: function () {
        var me = this;
        me.left = me.getClassTree();
        me.center = me.getGrid();
        me.viewport.add([me.left, me.center]);
    }
});